{% extends 'base.html' %}

{% block title %}Search Results - Research Paper Analyzer{% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h3 class="mb-0">
                                <i class="fas fa-search me-2"></i>Search Results
                            </h3>
                            <p class="text-muted mb-0">
                                {% if search_query %}
                                    Query: <strong>"{{ search_query.query }}"</strong> | 
                                {% endif %}
                                Papers found: <strong>{{ total_papers }}</strong>
                                
                            </p>
                        </div>
                        <div class="btn-group">
                            <a href="{% url 'papers:export_csv' %}" class="btn btn-outline-success">
                                <i class="fas fa-file-csv me-1"></i>Export CSV
                            </a>
                            <a href="{% url 'papers:search' %}" class="btn btn-outline-primary">
                                <i class="fas fa-search me-1"></i>New Search
                            </a>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    {% if page_obj.object_list %}
                        <div class="row">
                            {% for paper in page_obj.object_list %}
                                <div class="col-lg-6 mb-4">
                                    <div class="card h-100 border">
                                        <div class="card-header bg-light">
                                            <h6 class="mb-0 text-primary">{{ paper.title|truncatechars:80 }}</h6>
                                        </div>
                                        <div class="card-body">
                                            <div class="row mb-2">
                                                <div class="col-6">
                                                    <small class="text-muted">
                                                        <i class="fas fa-users me-1"></i>
                                                        {{ paper.authors|truncatechars:40 }}
                                                    </small>
                                                </div>
                                                <div class="col-6 text-end">
                                                    <small class="text-muted">
                                                        <i class="fas fa-calendar me-1"></i>
                                                        {{ paper.publication_date.year|default:"N/A" }}
                                                    </small>
                                                </div>
                                            </div>
                                            
                                            <p class="card-text small">
                                                {{ paper.abstract|truncatewords:30 }}
                                            </p>
                                            
                                            <div class="mt-auto d-flex justify-content-between align-items-center">
                                                <div>
                                                    <span class="badge bg-secondary">{{ paper.source|default:"Unknown" }}</span>
                                                    {% if paper.citation_count %}
                                                        <span class="badge bg-info">{{ paper.citation_count }} citations</span>
                                                    {% endif %}
                                                </div>
                                                <div>
                                                    {% if paper.doi %}
                                                        <a href="https://doi.org/{{ paper.doi }}" target="_blank" class="btn btn-sm btn-outline-success me-1">
                                                            <i class="fas fa-external-link-alt"></i> DOI
                                                        </a>
                                                    {% elif paper.arxiv_id %}
                                                        <a href="https://arxiv.org/abs/{{ paper.arxiv_id }}" target="_blank" class="btn btn-sm btn-outline-success me-1">
                                                            <i class="fas fa-external-link-alt"></i> ArXiv
                                                        </a>
                                                    {% elif paper.pmid %}
                                                        <a href="https://pubmed.ncbi.nlm.nih.gov/{{ paper.pmid }}/" target="_blank" class="btn btn-sm btn-outline-success me-1">
                                                            <i class="fas fa-external-link-alt"></i> PubMed
                                                        </a>
                                                    {% elif paper.source_url %}
                                                        <a href="{{ paper.source_url }}" target="_blank" class="btn btn-sm btn-outline-success me-1">
                                                            <i class="fas fa-external-link-alt"></i> Source
                                                        </a>
                                                    {% endif %}
                                                    <a href="{% url 'papers:detail' paper.id %}" class="btn btn-sm btn-outline-primary">
                                                        <i class="fas fa-eye"></i> View
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                        
                        <!-- Simple Pagination -->
                        {% if page_obj.has_other_pages %}
                        <nav aria-label="Search results pagination">
                            <ul class="pagination justify-content-center">
                                {% if page_obj.has_previous %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
                                    </li>
                                {% endif %}
                                
                                <li class="page-item active">
                                    <span class="page-link">{{ page_obj.number }}</span>
                                </li>
                                
                                {% if page_obj.has_next %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
                                    </li>
                                {% endif %}
                            </ul>
                        </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-search fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">No papers found</h5>
                            <p class="text-muted">Try adjusting your search query or parameters.</p>
                            <a href="{% url 'papers:search' %}" class="btn btn-primary">
                                <i class="fas fa-search me-1"></i>Try New Search
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- Export Section -->
            <div class="card mt-4">
                <div class="card-header">
                    <h5 class="mb-0">
                        <i class="fas fa-download me-2"></i>Export Results
                    </h5>
                </div>
                <div class="card-body">
                    <a href="{% url 'papers:export_csv' %}" class="btn btn-success me-2">
                        <i class="fas fa-file-csv me-1"></i>Download CSV
                    </a>
                    <a href="{% url 'papers:search' %}" class="btn btn-primary">
                        <i class="fas fa-search me-1"></i>New Search
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}